import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//二叉树层序遍历  力扣102
class Solution9 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> Tree = new ArrayList<List<Integer>>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        int levelsize = 0;
        if(root==null){
            return Tree;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            levelsize = queue.size();
            List<Integer> dfs = new ArrayList<Integer>();
            for(int i=0;i<levelsize;i++){
                TreeNode cur = queue.poll();
                dfs.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            Tree.add(dfs);
        }
        return Tree;
    }
}